﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'ProjectLabour.Grid',
    {
        extend: 'Ext.grid.Panel',

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '->',
                                {
                                    iconCls: 'icon-approve',
                                    text: '批准',
                                    itemId: 'button-approve',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '批准养护工程量批准申请',
                                    handler: this.onApproveClick
                                },
                                {
                                    iconCls: 'icon-reject',
                                    text: '拒绝',
                                    itemId: 'button-reject',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '拒绝养护工程量批准申请',
                                    handler: this.onRejectClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中
            if (selections.length === 0)
            {
                //禁用删除按钮
                this.down('#button-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#button-edit').setDisabled(true);
                //禁用上传按钮
                this.down('#button-submit').setDisabled(true);
            }
            else
            {
                //启用删除按钮
                this.down('#button-delete').setDisabled(false);
                //启用编辑按钮
                this.down('#button-edit').setDisabled(false);
                //启用上传按钮
                this.down('#button-submit').setDisabled(false);
            }
        },

        //处理选中养护变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中养护
            if (selections.length === 0)
            {
                //禁用批准按钮
                this.down('#button-approve').setDisabled(true);
                //禁用拒绝按钮
                this.down('#button-reject').setDisabled(true);
            }
            else
            {
                //获得选中的行
                var selection = this.getView().getSelectionModel().getSelection()[0];

                //启用批准按钮
                this.down('#button-approve').setDisabled(false);
                //启用拒绝按钮
                this.down('#button-reject').setDisabled(false);
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理批准按钮点击事件
        onApproveClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得所有选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //记录所有的编号
                var labours = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    labours.push(selection[index].data.LabourID);
                }

                //判定窗体是否已经创建
                if (!this.win_accept)
                {
                    //创建窗体
                    this.win_accept = Ext.create
                    (
                        'Ext.window.Window',
                        {
                            title: '工程人工批准请求批准',
                            maximizable: false,
                            maximized: false,
                            width: 480,
                            height: 320,
                            autoScroll: false,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //查询备注控件
                                    var txtComment = win.down('#txtComment');

                                    //判断是否存在备注控件
                                    if (txtComment)
                                    {
                                        //设置备注
                                        txtComment.setValue(win.Comment);
                                    }
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    defaultType: 'textfield',
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%'
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'textareafield',
                                            name: 'comment',
                                            itemId: 'txtComment',
                                            emptyText: '审核意见（最长400个字符）',
                                            maxLength: 400,
                                            height: '100%'
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form');

                                                //获得窗体
                                                var win = this.up('window');

                                                //检查数据是否合法
                                                if (form.getForm().isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('ExecuteConfirm'),
                                                            method: HttpMethod.Post,
                                                            params: { labours: win.Data, passed: true },
                                                            scope: win,
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置数据
                this.win_accept.Data = labours;

                //设置文字
                this.win_accept.Comment = selection[0].data.Comment;

                //显示窗体
                this.win_accept.show();
            }
        },

        //处理拒绝按钮点击事件
        onRejectClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得所有选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //记录所有的编号
                var labours = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    labours.push(selection[index].data.LabourID);
                }

                //判定窗体是否已经创建
                if (!this.win_reject)
                {
                    //创建窗体
                    this.win_reject = Ext.create
                    (
                        'Ext.window.Window',
                        {
                            title: '工程人工批准请求拒绝',
                            maximizable: false,
                            maximized: false,
                            width: 480,
                            height: 320,
                            autoScroll: false,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //查询备注控件
                                    var txtComment = win.down('#txtComment');

                                    //判断是否存在备注控件
                                    if (txtComment)
                                    {
                                        //设置备注
                                        txtComment.setValue(win.Comment);
                                    }
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    defaultType: 'textfield',
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%'
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'textareafield',
                                            name: 'comment',
                                            itemId: 'txtComment',
                                            emptyText: '审核意见（最长400个字符）',
                                            maxLength: 400,
                                            height: '100%'
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form');

                                                //获得窗体
                                                var win = this.up('window');

                                                //检查数据是否合法
                                                if (form.getForm().isValid())
                                                {
                                                    //提交数据
                                                    form.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('ExecuteConfirm'),
                                                            method: HttpMethod.Post,
                                                            params: { labours: win.Data, passed: false },
                                                            scope: win,
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置数据
                this.win_reject.Data = labours;

                //设置文字
                this.win_reject.Comment = selection[0].data.Comment;

                //显示窗体
                this.win_reject.show();
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _Current_ActionUrl }));

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'ProjectLabourModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('ConfirmSelect'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //创建选择模型
        var selectionModel = Ext.create('Ext.selection.CheckboxModel');

        //创建表格
        Ext.create
        (
            'ProjectLabour.Grid',
            {
                store: store,
                selModel: selectionModel,
                stateful: true,
                stateId: 'projectlabour-confirm-state-grid',
                height: 600,
                width: '100%',
                id: 'projectlabour-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '单位名称',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Organization',
                        hideable: false
                    },
                    {
                        text: '项目编号',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectCode',
                        hideable: false
                    },
                    {
                        text: '项目名称',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectName',
                        hideable: false
                    },
                    {
                        text: '状态',
                        width: 60,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'State',
                        renderer: Common.RenderProjectDataState
                    },
                    {
                        text: '审核意见',
                        xtype: 'templatecolumn',
                        tpl: '<a class="Comment" href="javascript:void(0);" onclick="javascript:return Common.ViewComment(this);" title="{Comment}">{Comment}</a> ',
                        width: 80,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Comment',
                        hideable: true
                    },
                    {
                        text: '人工属性',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'LabourType'
                    },
                    {
                        text: '工种',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Job'
                    },
                    {
                        text: '人工数量',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Quantity'
                    },
                    {
                        text: '人工单价',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Price'
                    },
                    {
                        text: '小计',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Amount'
                    },
                    {
                        text: '施工日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'ApplicationDate'
                    },
                    {
                        text: '填报人',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Applicant'
                    },
                    {
                        text: '填报日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '审核人',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Auditor'
                    },
                    {
                        text: '审核日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'AuditedDate'
                    },
                    {
                        text: '签审人',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Signer'
                    },
                    {
                        text: '签审日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'SignedDate'
                    },
                    {
                        text: '延期填报',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        renderer: renderGridReadonlyCheckBox,
                        dataIndex: 'IsDelay'
                    },
                    {
                        text: '备注',
                        xtype: 'templatecolumn',
                        tpl: '<span title="{Memo}">{Memo}</span> ',
                        width: 160,
                        minWidth: 160,
                        flex: 1,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Memo',
                        hideable: true
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('projectlabour-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('projectlabour-grid', 'body_content');
    }
);
